# koroFileHeader at Yungoal acer
# Create: 2021-09-07 16:22:44
# LastEdit: 2021-09-07 17:24:26
"""https://leetcode-cn.com/problems/merge-sorted-array/"""
__author__ = '749B'

from typing import List

class Solution:
    def merge0(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
        """
        Do not return anything, modify nums1 in-place instead.
        """
        nums1[m:] = nums2
        nums1.sort()

    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
        """逆向双指针，还能防止nums1中的元素被覆盖"""
        while m and n:
            if nums1[m - 1] < nums2[n - 1]:
                nums1[m + n - 1] = nums2[n - 1]
                n -= 1
            else:
                nums1[m + n - 1] = nums1[m - 1]
                m -= 1
        if m == 0:
            nums1[:n] = nums2[:n]
            


if __name__ == '__main__':
    nums1 = [1,2,3,0,0,0]
    m = 3
    nums2 = [2,5,6]
    n = 3
    Solution().merge(nums1, m, nums2, n)
    print(nums1)

